PDB_DIR = pdb
PROGRAM_DIR = program

codes = $(basename $(notdir $(wildcard ${PDB_DIR}/????.pdb)))

reverted = $(addsuffix .pdb, $(wildcard ${PDB_DIR}/????.pdb))
cleaned = $(addsuffix .new, $(wildcard ${PDB_DIR}/????.pdb))
contacts = $(addsuffix .hb2, $(wildcard ${PDB_DIR}/????.pdb)) $(addsuffix .nb2, $(wildcard ${PDB_DIR}/????.pdb))

all: hbclean interfaces

revert: ${reverted}

hbclean: ${cleaned}

hbcontacts: ${contacts}

database: db/mergerd.sqlite

interfaces: db/with_interfaces.sqlite

db/with_interfaces.sqlite: db/merged.sqlite
	cp db/merged.sqlite db/with_interfaces.sqlite
	./program/find_interfaces.py db/with_interfaces.sqlite ${PDB_DIR}

db/mergerd.sqlite: db/nonmerged.sqlite
	cp db/nonmerged.sqlite db/merged.sqlite
	./program/db_cleanup.py db/merged.sqlite

db/nonmerged.sqlite: ${contacts}
	./program/contacts.py ${contacts}
	mv db.sqlite db/nonmerged.sqlite

${PDB_DIR}/%.pdb.hb2 ${PDB_DIR}/%.pdb.nb2: ${PDB_DIR}/%.pdb.pdb
	 -cd pdb; ../program/hbplus.sh $(basename $(notdir $<))

${PDB_DIR}/%.pdb.new: ${PDB_DIR}/%.pdb.pdb
	cd pdb; ../program/clean.sh $(notdir $<)
	
${PDB_DIR}/%.pdb.pdb: ${PDB_DIR}/%.pdb
	./program/pdb_reverter.py $<

clean:
	rm -f ${PDB_DIR}/*.pdb.pdb
	rm -f ${PDB_DIR}/*.?b2
	rm -f ${PDB_DIR}/*.pdb.h
	rm -f ${PDB_DIR}/*.pdb.new
	rm -f ${PDB_DIR}/*.pdb.hblog
	rm -f ${PDB_DIR}/*.pdb.alt
	rm -f ${PDB_DIR}/*.pdb.cleanlog

	rm -f ${PROGRAM_DIR}/db.sqlite
	rm -f ${PROGRAM_DIR}/testdb.sqlite
	rm -f ${PROGRAM_DIR}/TEST.hb2
	
	rm -f ${DB_DIR}/nonmerged.sqlite
	rm -f ${DB_DIR}/merged.sqlite
	rm -f ${DB_DIR}/with_interfaces.sqlite
